#pragma once
#include<iostream>
#include<string>
#include<functional>

#include"InetAddr.hpp"
// #include"Reactor.hpp"
class Reactor;

#define ListenConnection 0
#define NormalConnection 1
class Connection;
using handler_t = std::function<void(Connection *conn)>;
class Connection
{
public:
    Connection(int sockfd):_sockfd(sockfd)
    {}
    void RegisterHandler(handler_t recver,handler_t sender,handler_t excepter)
    {
        _handler_recver=recver;
        _handler_sender=sender;
        _handler_excepter=excepter;
    }
    void SetEvents(uint32_t events)
    {
        _events=events;
    }
    uint32_t GetEvents()
    {
        return _events;
    }
    void SetReactor(Reactor* R)
    {
        _R=R;
    }
    int Sockfd()
    {
        return _sockfd;
    }
    void SetConnectionType(int type)
    {
        _type=type;
    }
    int Type()
    {
        return _type;
    }
    void SetAddr(InetAddr addr)
    {
        _addr=addr;
    }
    void AppendInBuff(const std::string in)
    {
        _inbuffer+=in;
    }
    std::string& InBuffer()
    {
        return _inbuffer;
    }
    void AppendOutBuffer(const std::string in)
    {
        _outbuffer+=in;
    }
    std::string& OutBuffer()
    {
        return _outbuffer;
    }
    void DiscardOutbuffer(int n)
    {
        _outbuffer.erase(0,n);
    }
    void Close()
    {
        if(_sockfd>0)
        ::close(_sockfd);
    }
    ~Connection(){}
private:
    int _sockfd;
    uint32_t _events;
    std::string _inbuffer;
    std::string _outbuffer;
    int _type;
public:
    handler_t _handler_recver;
    handler_t _handler_sender;
    handler_t _handler_excepter;
    InetAddr _addr;

    Reactor* _R;//回指向自己所属的reactor
};